﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_MyMessageBoardListCopy : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //tbDateTime.Attributes.Add("onclick", "WdatePicker()");
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        base.OnLoad(e);
    }

    #endregion

    private void BindData()
    {
        PagedDataSource Pds = new PagedDataSource();

        //string Condition = "([CustomerID] = " + _CustomerID.ToString() + " or ToCustomerID = " + _CustomerID.ToString() + ") and ShopID = " + _Site.ID.ToString();
        string Condition = "([CustomerID] = " + _Customer.ID.ToString() + " or ToCustomerID = " + _Customer.ID.ToString() + ")";

        //if (ddlReply.SelectedValue == "1")
        //{
        //    if (Condition != "")
        //    {
        //        Condition += " and ReplyDateTime = [DateTime]";
        //    }
        //    else
        //    {
        //        Condition = "ReplyDateTime = [DateTime]";
        //    }
        //}
        //else
        //{
        //    if (Condition != "")
        //    {
        //        Condition += " and ReplyDateTime > [DateTime]";
        //    }
        //    else
        //    {
        //        Condition = "ReplyDateTime > [DateTime]";
        //    }
        //}

        //if (tbKeyWord.Value != "请输入搜索的关键字")
        //{
        //    if (Condition != "")
        //    {
        //        Condition += " and (Title like '%" + tbKeyWord.Value + "%')";
        //    }
        //    else
        //    {
        //        Condition = "(Title like '%" + tbKeyWord.Value + "%')";
        //    }
        //}

        //if (tbDateTime.Value != "")
        //{
        //    int TimeType = Shove._Convert.StrToInt(ddlTimeType.SelectedValue, 0);
        //    DateTime dTime = Shove._Convert.StrToDateTime(tbDateTime.Value.Trim(), DateTime.Now.ToShortDateString());

        //    if (Condition != "")
        //    {
        //        if (TimeType == 1)
        //        {
        //            Condition += " and datediff(dd, [DateTime], '" + dTime.ToString() + "') = 0";
        //        }

        //        if (TimeType == 2)
        //        {
        //            Condition += " and datediff(mm, [DateTime], '" + dTime.ToString() + "') = 0";
        //        }

        //        if (TimeType == 3)
        //        {
        //            Condition += " and datediff(yy, [DateTime], '" + dTime.ToString() + "') = 0";
        //        }
        //    }
        //    else
        //    {
        //        if (TimeType == 1)
        //        {
        //            Condition = "datediff(dd, [DateTime], '" + dTime.ToString() + "') = 0";
        //        }

        //        if (TimeType == 2)
        //        {
        //            Condition = "datediff(mm, [DateTime], '" + dTime.ToString() + "') = 0";
        //        }

        //        if (TimeType == 3)
        //        {
        //            Condition = "datediff(yy, [DateTime], '" + dTime.ToString() + "') = 0";
        //        }
        //    }
        //}

        DataTable dt = new DAL.Views.V_MessageBoardCopy().Open("",Condition, "[DateTime] desc");

        if (dt!=null && dt.Rows.Count > 0)
        {
            dt.Columns.Add("Content2", System.Type.GetType("System.String"));
            dt.Columns.Add("Reply2", System.Type.GetType("System.String"));

            foreach (DataRow dr in dt.Rows)
            {
                string Content = "", Reply = "";
                try
                {
                    Content = Shove._Convert.ToHtmlCode(Shove._String.Decompress((byte[])dr["Content"]));
                }
                catch { }
                try
                {
                    Reply = Shove._Convert.ToHtmlCode(Shove._String.Decompress((byte[])dr["Reply"]));
                }
                catch { }

                dr["Content2"] = Content;
                dr["Reply2"] = Reply;

                dt.AcceptChanges();
            }
        }

        Pds.DataSource = dt.DefaultView;
        Pds.AllowPaging = true; //开启分页
        Pds.PageSize = 5; //分页
        PSize.Text = Pds.PageSize.ToString();

        int index = int.Parse(PageIndex.Text); //当前页数
        int PageSize = Pds.PageSize;
        int TotalRecords = Pds.DataSourceCount;
        int TotalPages = TotalRecords / PageSize;//得到总页数（总记录数除以每页的记录数） 
        int ModePages = TotalRecords % PageSize;//得到取模的余数(总记录数取模每页的记录数) 

        PageColumn.Visible = (dt.Rows.Count > 0);

        if (ModePages > 0)//如果取模数不等于0，则把总页数加1 
        {
            TotalPages += 1;
        }
        if (TotalPages == 0)
        {
            TotalPages = 1;
        }
        if (index > TotalPages)
        {
            index = TotalPages;
            PageIndex.Text = index.ToString();
        }

        MaxPage.Text = TotalPages.ToString();//显示页面上的总页数 
        TotalCount.Text = TotalRecords.ToString();//显示页面上的总记录数 
        Pds.CurrentPageIndex = index - 1;

        if (index == 1)
        {
            lnkPrev.Enabled = false;
            if (TotalPages > 1)
            {
                lnkNext.Enabled = true;
            }
            else
            {
                lnkNext.Enabled = false;
            }
        }
        else
        {
            lnkPrev.Enabled = true;

            if (index == TotalPages)
            {
                lnkNext.Enabled = false;
            }
            else
            {
                lnkNext.Enabled = true;
            }
        }

        selectPage.Items.Clear(); //清除防止累加

        for (int i = 1; i <= TotalPages; i++)
        {
            selectPage.Items.Add(i.ToString());
        }

        selectPage.Items.FindByText(index.ToString()).Selected = true; //把当前页显示在列表框的第一个
        g.DataSource = Pds;
        g.DataBind();
    }

    protected void lnkPrev_Click(object sender, EventArgs e)
    {
        int index = int.Parse(PageIndex.Text);
        index--;
        PageIndex.Text = index.ToString();
        BindData();
    }

    protected void lnkNext_Click(object sender, EventArgs e)
    {
        int index = int.Parse(PageIndex.Text);
        index++;
        PageIndex.Text = index.ToString();
        BindData();
    }

    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        PageIndex.Text = selectPage.SelectedValue;
        BindData();
    }

    protected void g_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "btnReply")
        {
            System.Web.UI.HtmlControls.HtmlInputHidden tbID = (System.Web.UI.HtmlControls.HtmlInputHidden)e.Item.FindControl("tbID");
            System.Web.UI.WebControls.TextBox tbReply = (System.Web.UI.WebControls.TextBox)e.Item.FindControl("tbReply");

            DAL.Tables.tbl_MessageBoardCopy MessageBoard = new DAL.Tables.tbl_MessageBoardCopy();

            MessageBoard.Reply.Value = Shove._String.Compress(tbReply.Text);
            MessageBoard.ReplyDateTime.Value = DateTime.Now;

            if (MessageBoard.Update("[id] = " + tbID.Value) < 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "数据保存异常，请重新提交。");

                return;
            }

            BindData();

            Shove._Web.JavaScript.Alert(this.Page, "回复保存成功。");

            return;
        }

        if (e.CommandName == "btnDelete")
        {
            System.Web.UI.HtmlControls.HtmlInputHidden tbID = (System.Web.UI.HtmlControls.HtmlInputHidden)e.Item.FindControl("tbID");

            DAL.Tables.tbl_MessageBoardCopy MessageBoard = new DAL.Tables.tbl_MessageBoardCopy();

            if (MessageBoard.Delete("[id] = " + tbID.Value) < 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "汇款通知删除错误");

                return;
            }

            Shove._Web.JavaScript.Alert(this.Page, "汇款通知删除成功。");

            BindData();

            return;
        }
    }

    protected void g_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        System.Web.UI.HtmlControls.HtmlInputHidden tbToCustomerID = (System.Web.UI.HtmlControls.HtmlInputHidden)e.Item.FindControl("tbToCustomerID");
        Button Button1 = (Button)e.Item.FindControl("Button1");
        Button Button2 = (Button)e.Item.FindControl("Button2");
        TextBox tbReply = (TextBox)e.Item.FindControl("tbReply");

        if (tbToCustomerID != null)
        {
            if ((Shove._Convert.StrToLong(tbToCustomerID.Value, -1) != _Customer.ID))
            {
                Button1.Enabled = false;
                Button2.Enabled = false;
                tbReply.Enabled = false;
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnOK_Click(object sender, System.EventArgs e)
    {
        if (tbTitle.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入汇款人。");
            return;
        }

        if (tbContent.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入通知内容。");
            return;
        }

        long ToCustomerID = 1;

        if (tbToCustomer.Text.Trim() != "")
        {
            Customer tc = new Customer(_Site.ID);

            tc.CustomerNO = tbToCustomer.Text.Trim();

            string ReturnDescription = "";

            if (tc.GetCustomerInformationByNO(ref ReturnDescription) < 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "你输入的用户不存在");

                return;
            }

            ToCustomerID = tc.ID;

            if (ToCustomerID == _Customer.ID)
            {
                Shove._Web.JavaScript.Alert(this.Page, "不能将通知发给自己");

                return;
            }
        }

        DAL.Tables.tbl_MessageBoardCopy MessageBoard = new DAL.Tables.tbl_MessageBoardCopy();

        MessageBoard.ShopID.Value = _Site.ID;
        MessageBoard.CustomerID.Value = _Customer.ID;
        MessageBoard.Title.Value = tbTitle.Text.Trim();
        MessageBoard.Content.Value = Shove._String.Compress(tbContent.Text);
        MessageBoard.IPAddress.Value = System.Web.HttpContext.Current.Request.UserHostAddress;
        MessageBoard.ToCustomerID.Value = ToCustomerID;

        if (MessageBoard.Insert() < 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "汇款通知提交失败");

            return;
        }

        tbTitle.Text = "";
        tbContent.Text = "";
        BindData();
        Shove._Web.JavaScript.Alert(this.Page, "汇款通知提交成功。");

        return;
    }
}